package com.casicloud.manage.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.casicloud.manage.model.Authority;
import com.casicloud.manage.model.Menu;
import com.casicloud.manage.model.User;
import com.casicloud.manage.model.UserRole;
import com.casicloud.manage.service.AuthorityService;
import com.casicloud.manage.service.MenuService;
import com.casicloud.manage.service.UserRoleService;
import com.casicloud.manage.service.UserService;
import com.common.sub.util.ControllerBase;
import com.common.sub.util.JsonUtils;
import com.common.sub.util.PageUtil;
import com.common.sub.util.UserRoleList;
import com.common.sub.util.ZtreeModel;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "user")
public class UserController extends ControllerBase {
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private AuthorityService authorityService;
	

	
	
//	private static final Log logger = LogFactory.getLog(UserController.class);
	
	
	
	@RequiresPermissions("user:query")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(HttpServletRequest request,HttpServletResponse response){
		return "user/list";
	}
	@RequiresPermissions("user:query")
	@RequestMapping(value = "query")
	public String query(User user,PageUtil pageUtil,HttpServletRequest request,HttpServletResponse response){
		PageInfo<User>	page  = userService.findByPage(pageUtil,user);
		return this.dataToJson(page);
	}
	
	
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String goupdate(User user,HttpServletRequest request,HttpServletResponse response){
		User u = userService.findByOne(user);
		request.setAttribute("u", u);
		return "user/update";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String doupdate(User user,HttpServletRequest request,HttpServletResponse response){
		user.setCreateTime(new Date());
		userService.update(user);
		
		return this.formSuccee();
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String goadd(User user,HttpServletRequest request,HttpServletResponse response){

		return "user/add";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String doadd(User user,HttpServletRequest request,HttpServletResponse response){
		user.setCreateTime(new Date());
		userService.save(user);
		return this.formSuccee();
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(User user,HttpServletRequest request,HttpServletResponse response){
		if(user.getId()!=null){
			User u = new User();
			u.setId(user.getId());
			userService.delete(u);
			return this.formSuccee();
		}else{
			return this.formFail();
		}
	}
	@RequestMapping(value = "updaterole")
	public String updaterole(User user,@RequestParam("roleId")Integer[] ids,HttpServletRequest request,HttpServletResponse response){
		userService.updateRole(user, ids);
		return this.formSuccee();
	}
	
	@RequestMapping(value = "permission")
	 public String permission(User user,HttpServletRequest request,HttpServletResponse response){
		
		UserRole ur = new UserRole();
		ur.setUserId(user.getId());
		List<UserRole> urlist = userRoleService.findRoleByList(ur);
		UserRoleList ul = new UserRoleList(urlist);
		
		List<Menu> mlist = menuService.findByIds(ul.getMenulist().toArray(new Integer[]{}));
		List<Authority> alist = authorityService.findByIds(ul.getAuthoritylist().toArray(new Integer[]{}));
		
		List<ZtreeModel> zlist = new ArrayList<ZtreeModel>();
		
		
		for(int i = 0;i<mlist.size();i++){
			Menu m= mlist.get(i);
			ZtreeModel zm = new ZtreeModel();
			zm.setId(m.getId());
			zm.setName(m.getName());
			zm.setIcon(m.getIcon());
			zm.setpId(m.getPid());
			zlist.add(zm);
		}
		
		for(int i = 0;i<alist.size();i++){
			ZtreeModel zm = new ZtreeModel();
			Authority a = alist.get(i);
			zm.setId(0-a.getId());
			zm.setName(a.getName());
			zm.setIcon("img/icon/3.png");
			zm.setpId(a.getMenuId());
			zlist.add(zm);
			
		}
		request.setAttribute("json",JsonUtils.ObjectToString(zlist));
		
		
		
		return "user/permission";
	 }
	
	
	
	
}
